var layer;
var $;
var element;
var util;
var table;
var laydate;
var tree;
layui.use(['element','table','layer','util','jquery','laydate','tree'], function(){
    layer = layui.layer;
    $ = layui.jquery;
    element = layui.element;
    util = layui.util;
    table = layui.table;
    laydate = layui.laydate;
    tree = layui.tree;
    laydate.render({
        elem: '#createDate',
        type: 'date',
        trigger: 'click'
    });
    laydate.render({
        elem: '#gainDate',
        type: 'date',
        trigger: 'click'
    });

    selSparesTable('','','','','');
});
function selSparesTable(name,createUserName,categoryName,createDate,gainDate){
    $.ajax({
        url:"/articletype/selSparesTable",
        dataTypr:"post",
        type:"post",
        data:{"name":name,"createUserName":createUserName,"categoryName":categoryName,"createDate":createDate,"gainDate":gainDate},
        success:function(data){
            getTable(data);
        }
    })
}
function getTable(data){
    table.render({
        elem: '#test1',
        page: true ,
        data:data,
        width:930,
        even:true,
        limits:[5,7,10],
        limit:9,
        cols:[[
            {type:'radio',fixed:'left'},
            {title:'序号',align:'center',width:80,templet:function(data){
                    return data.LAY_INDEX;
                }},
            {field: 'custName', title: '名称',align:'center',width:140},
            {field: 'createUserName', title: '联系人',align:'center',width:120},
            {field: 'createDate', title: '联系人电话',align:'center',width:160},
            {field: 'categoryName', title: '投诉',align:'center',width:140},
            {field: '', title: '服务',align:'center',width:90},
            {field: 'status', title: '地址',align:'center',width:140}
        ]]
    });
    //表格监听事件
    table.on("row(test1)",function(obj){
        $("#sparesName").val(obj.data.name);
        $("#sparesId").val(obj.data.id);
        $("#categoryId").val(obj.data.categoryId)
    })
    //模糊查询
    $("#chaxun").on('click',function(){
        //获取值
        var name = $("input[name=name]").val();
        var createUserName = $("input[name=createUserName]").val();
        var categoryName = $("input[name=categoryName]").val();
        var createDate = $("input[name=createDate]").val();
        var gainDate = $("input[name=gainDate]").val();
        selSparesTable(name,createUserName,categoryName,createDate,gainDate);
    })
    //确定提交
    $("#queding").on('click',function(){
        var sparesName = $("#sparesName").val();
        var sparesId = $("#sparesId").val();
        var categoryId = $("#categoryId").val();
        parent.$("input[name=categoryId]").val(categoryId);
        parent.$("input[name=sampleModel]").val(sparesName);
        parent.$("input[name=sampleModelId]").val(sparesId);
        //得到父关闭
        var layer=parent.layer;
        layer.close(layer.index);
    })
    //取消
    $("#quxiao").on('click',function(){
        var layer=parent.layer;
        layer.close(layer.index);
    })
}
function findCreateUserName(){
    layer.open({
        type : 2,//寮瑰嚭灞傜殑绫诲瀷
        title : "鐧昏浜轰俊鎭�",
        area : [ '780px', '550px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"/add-table-handler.html",
        btn:['纭畾','鍙栨秷'],
        yes:function(index,layero){
            var obj = layero.find('iframe').contents();
            $("input[name=createUserName]").val(obj.find('#jingshourenming').val());
            $("input[name=createUser]").val(obj.find('#jingshourenid').val());
            layer.close(index);
        }
    })
}
function findCategoryName(){
    layer.open({
        type : 1,//寮瑰嚭灞傜殑绫诲瀷
        title : "鏍峰搧绫诲瀷",
        area : [ '450px', '300px' ],
        maxmin: true,
        shade : 0,
        offset: 'auto',
        content :"<div id='categoryTree'></div>",
        btn:['纭畾','鍙栨秷'],
        yes:function(index,layero){
            var categoryName = $("#cName").val();
            var id = $("#cId").val();
            $("input[name=categoryName]").val(categoryName);
            $("input[name=categoryId]").val(id);
            layer.close(index);
        },
        btn2:function(){

        },
        success:function(){
            $.ajax({
                url:"/articletype/selCategoryTree",
                dataTypr:"post",
                type:"post",
                data:{},
                success:function(data){
                    tree.render({
                        elem: "#categoryTree",//瀹瑰櫒
                        data: data,
                        showCheckbox: true,//灞曠ず澶嶉€夋
                        oncheck: function (obj) {
                            var title = obj.data.title;
                            var id = obj.data.id;
                            var categoryName = title.substring(0,title.indexOf("["));
                            $("#cName").val(categoryName);
                            $("#cId").val(id);
                        }
                    })
                }
            })
        }
    })
}